﻿using System;

namespace Shinkansen.Configuration
{
    public class CryptoProviderBuilder
    {
        private static readonly CryptoProviderBuilder _instance = new CryptoProviderBuilder ();
        private Func<ICryptoKeyProvider> _providerFactory;

        /// <summary>
        /// Gets the current.
        /// </summary>
        /// <value>The current.</value>
        public static CryptoProviderBuilder Current
        {
            get { return _instance; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CryptoProviderBuilder"/> class.
        /// </summary>
        public CryptoProviderBuilder ()
        {
            Reset ();
        }

        /// <summary>
        /// Resets this instance and sets default prodiver factory.
        /// </summary>
        public void Reset ()
        {
            _providerFactory = () => new MachineKeyCryptoProvider ();
        }

        /// <summary>
        /// Gets the crypto provider.
        /// </summary>
        /// <returns></returns>
        public ICryptoKeyProvider GetCryptoProvider ()
        {
            return _providerFactory ();
        }

        /// <summary>
        /// Sets the crypto provider.
        /// </summary>
        /// <param name="cryptoKeyProvider">The crypto key provider.</param>
        public void SetCryptoProvider (ICryptoKeyProvider cryptoKeyProvider)
        {
            if (cryptoKeyProvider == null)
                throw new ArgumentNullException ("cryptoKeyProvider");

            _providerFactory = () => cryptoKeyProvider;
        }
    }
}
